package com.ssm.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * 必须在 spring.factories 中配置
 */
public class MyApplicationRunListener implements SpringApplicationRunListener {

    public MyApplicationRunListener(SpringApplication application, String[] args) {
        System.out.println("constructor");
    }

    @Override
    public void starting() {
        System.out.println("MyApplicationRunListener-starting...");
        System.out.println("在 run() 方法开始执行时，该方法就立即被调用，可用于在初始化最早期时做一些工作");
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        System.out.println("MyApplicationRunListener-environmentPrepared...");
        System.out.println("当 environment 构建完成，ApplicationContext 创建之前，该方法被调用");
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("MyApplicationRunListener-contextPrepared...");
        System.out.println("当 ApplicationContext 构建完成时，该方法被调用");
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("MyApplicationRunListener-contextLoaded...");
        System.out.println("在 ApplicationContext 完成加载，但没有被刷新前，该方法被调用");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        System.out.println("MyApplicationRunListener-started...");
        System.out.println("在 ApplicationContext 刷新并启动后，CommandLineRunners 和 ApplicationRunner 未被调用前，该方法被调用");
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        System.out.println("MyApplicationRunListener-running...");
        System.out.println("在 run() 方法执行完成前该方法被调用");
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("MyApplicationRunListener-failed...");
        System.out.println("当应用运行出错时该方法被调用");
    }
}